package com.citycloud.ccuap.tc.sync.handle;

import com.alibaba.fastjson.JSON;
import com.citycloud.ccuap.commons.application.ApplicationContextProvider;
import org.apache.activemq.command.ActiveMQTopic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.core.JmsTemplate;

public class ActiveMqHandle implements MessageHandleFactory {

    private static Logger logger = LoggerFactory.getLogger(ActiveMqHandle.class);

    private final JmsTemplate jmsTemplate = (JmsTemplate) ApplicationContextProvider.getBean("jmsTemplate");

    @Override
    public void sendMessage(String topicName, Object message) {
        ActiveMQTopic topic = new ActiveMQTopic(topicName);

        String messageJson = JSON.toJSONString(message);
        logger.info("------------MQ send message:{},topicName 名称：{}", messageJson, topicName);

        jmsTemplate.send(topic, session -> session.createTextMessage(messageJson));
    }
}
